package ace.module.common.core.autoconfig;

import ace.cmp.security.oauth2.resource.server.autoconfig.config.Oauth2ResourceServerSecurityServiceAutoConfigure;
import ace.cmp.security.oauth2.resource.server.core.properties.Oauth2ResourceServerSecurityProperties;
import ace.cmp.security.oauth2.resource.server.core.service.Oauth2SecurityService;
import ace.cmp.security.oauth2.resource.server.core.service.impl.Oauth2ResourceServerSecurityServiceLoginSupplier;
import ace.cmp.security.oauth2.resource.server.core.service.impl.Oauth2ResourceServerSpringSecurityService;
import ace.module.common.core.impl.properties.ModuleCommonSecurityProperties;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * .
 * @author caspar
 */
@EnableConfigurationProperties({
    OAuth2ResourceServerProperties.class,
    Oauth2ResourceServerSecurityProperties.class,
    ModuleCommonSecurityProperties.class
})
@AutoConfigureBefore({Oauth2ResourceServerSecurityServiceAutoConfigure.class})
@Configuration
public class ModuleCommonSecurityAutoConfig {

  @Bean("securityService")
  @ConditionalOnMissingBean
  public Oauth2SecurityService securityService(Oauth2ResourceServerSecurityServiceLoginSupplier oauth2ResourceServerSecurityServiceLoginSupplier,
      ModuleCommonSecurityProperties moduleCommonSecurityProperties
  ) {
    Oauth2SecurityService securityService = new Oauth2ResourceServerSpringSecurityService(oauth2ResourceServerSecurityServiceLoginSupplier,
        moduleCommonSecurityProperties.getRolePrefix(),
        moduleCommonSecurityProperties.getAdministratorRoleKeySuffix()
    );
    return securityService;
  }
}
